// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
//引入elementui
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
//引入cookie
import cookie from 'vue-cookie';
//全局使用ElementuiUI
Vue.use(ElementUI);

import axios from 'axios'

Vue.prototype.$axios=axios;

Vue.prototype.$cookie=cookie;
axios.defaults.baseURL="http://localhost:7000/"

Vue.config.productionTip = false




//全局的路由守卫，判断当前cookie中是否有用户的token,如果没有则不允许进入其他页面，只能在登录页面
//isAuthenticated 是用户是否登录的变量
let isAuthenticated=false;
//从cookie中获取到用户的token
router.beforeEach((to, from, next) => {

  console.log(to);
  console.log(from);

//如果token不为nuLl且不为空字符串，则当前用户已经登录\
  let token = cookie.get("token");
  if (token!=null&&token!=''){
    isAuthenticated=true;
  }
//将token串设置到axios的header中,每次请求时，都携带token向后台发起请求
  axios.defaults.headers={
    "token":token
  }

  if (to.name !== 'HelloWorld' && !isAuthenticated){
    alert("当前用户未登录！")
    next({ name: 'HelloWorld' })
  }

  // 如果用户未能验证身份，则 `next` 会被调用两次
  next()
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
